﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace SolutionCrapFinder
{
    public static class Miscellaneous
    {
        static public void ToLower(List<string> list)
        {
            for (int i = 0; i < list.Count; ++i)
            {
                list[i] = list[i].ToLower();
            }
        }

        public static IEnumerable<string> GetSplittedLines(string strToSplit, char delimiter)
        {
            return strToSplit.Split(delimiter);
        }

        public static List<string> Intersect(IEnumerable<string> set1, IEnumerable<string> set2)
        {
            List<string> result = new List<string>();

            foreach (string s1 in set1)
            {
                foreach (string s2 in set2)
                {
                    if (0 == s1.CompareTo(s2))
                    {
                        result.Add(s1);
                    }
                }
            }

            return result;
        }

        public static void MoveFileToFolder(string fileName, string folder)
        {
            if (!File.Exists(fileName))
            {
                return;
            }

            string path = PathsHelper.RetrievePath(fileName) + "\\" + folder.Trim(new char[]{' ', '\\', '/'});
            string shortName = PathsHelper.RetrieveFileShortName(fileName);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);                
            }
            File.Move(fileName, path + '\\' + shortName);
        }
    }
    class XmlPathStack
    {
        // members
        Stack<string> m_stack = new Stack<string>();

        //methods
        public void Push(string elem)
        {
            m_stack.Push(elem);
        }
        public string Pop()
        {
            return m_stack.Pop();
        }
        public string GetElemByLevelReverse(int level)
        {
            int currLevel = 0;
            foreach (string elem in m_stack)
            {
                if (currLevel == level)
                    return elem;

                ++currLevel;
            }
            return "";
        }
    }

    class FileFilter
    {
        // members
        string[] m_filters = new string[]{};

        public FileFilter(string filterTokens)
        {
            if (filterTokens.Length != 0)
            {
                m_filters = filterTokens.Split(';');
                for (int i = 0; i < m_filters.Length; ++i)
                {
                    m_filters[i] = m_filters[i].Trim(new char[] { ' ', '.', '*' });
                }
            }
        }

        public bool IsFilterMatched(string fileName)
        {
            if (m_filters.Length == 0)
            {
                return true;
            }

            // TODO: filter must be improved
            string ext = Path.GetExtension(fileName).Trim(new char[]{' ', '.'});
            foreach (string s in m_filters)
            {
                if (0 == s.CompareTo(ext))
                {
                    return true;
                }
            }

            return false;
        }
    }

    class PathClipper
    {
        // members
        string m_path = "";
        //bool m_isNormalPath = true;

        public PathClipper(string path)
        {
            m_path = path;
            if (m_path == null)
                m_path = "";
        }

        public string GetPath()
        {
            return m_path;
        }

        public string ClipRight()
        {
            string res = "";
            int endPos = m_path.LastIndexOf('\\');
            if (endPos < 0)
                endPos = m_path.LastIndexOf('/');

            if (endPos < 0)
            {
                res = m_path;
                m_path = "";
            }
            else
            {
                res = m_path.Substring(endPos, m_path.Length - endPos);
                m_path = m_path.Substring(0, endPos);
            }

            return res;
        }

        public string ClipLeft()
        {
            string res = "";
            int startPos = m_path.IndexOf('\\');
            if (startPos < 0)
                startPos = m_path.IndexOf('/');

            if (startPos < 0)
            {
                res = m_path;
                m_path = "";
            }
            else
            {
                res = m_path.Substring(0, startPos);
                m_path = m_path.Substring(startPos + 1, m_path.Length - startPos - 1);
            }

            return res;
        }

        public void AddChunkRight(string chunk, char separator = '\\')
        {
            m_path += separator + chunk; 
        }
    }

    class PathsHelper
    {
        static public string RetrievePath(string fullFileName)
        {
            int endPos = fullFileName.LastIndexOf('\\');
            if (endPos < 0)
                endPos = fullFileName.LastIndexOf('/');

            if (endPos < 0)
                return "";

            return fullFileName.Substring(0, endPos);
        }

        static public string RetrieveFileShortName(string fullFileName)
        {
            int endPos = fullFileName.LastIndexOf('\\');
            if (endPos < 0)
                endPos = fullFileName.LastIndexOf('/');

            if (endPos < 0)
                return "";

            return fullFileName.Substring(endPos + 1, fullFileName.Length - endPos - 1);
        }

        static public string MakeFullPathFromRelative(string initialPath, string relPath)
        {
            if (Path.IsPathRooted(relPath))
                return relPath;
            if (relPath == null || relPath == "")
                return "";

            PathClipper relPathClipper = new PathClipper(relPath);
            PathClipper initPathClipper = new PathClipper(initialPath);
            string relChunk = relPathClipper.ClipLeft();
            while (relChunk.Length != 0)
            {
                if (relChunk == "..")
                {
                    initPathClipper.ClipRight();
                }
                else
                {
                    initPathClipper.AddChunkRight(relChunk);
                }

                relChunk = relPathClipper.ClipLeft();
            }

            return initPathClipper.GetPath();
        }

        static public string MakeFullPathFromRelativeEx(IEnumerable<string> initialPaths, string relPath)
        {
            foreach (string s in initialPaths)
            {
                string path = MakeFullPathFromRelative(s, relPath);
                if (File.Exists(path))
                    return path;
            }
            return "";
        }
    }
}
